package com.wanxi.service.impl;

import com.wanxi.dto.NewsDTO;
import com.wanxi.feign.NewsFeignClient;
import com.wanxi.mapper.NewsMapper;
import com.wanxi.model.NewsQuery;
import com.wanxi.result.CommonResult;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(description = "新闻服务")
@RestController
@RequestMapping("/news")
public class NewsServiceImpl implements NewsFeignClient {

    @Autowired
    NewsMapper newsMapper;

    private Logger logger = LoggerFactory.getLogger(getClass());

    @GetMapping("/findNews")
    @Override
    public CommonResult findNews(NewsQuery newsQuery) {
        return CommonResult.success(newsMapper.findNews(newsQuery));
    }

    @PostMapping("/addNews")
    @Override
    public CommonResult addNews(NewsDTO news) {
        logger.info("入参：" + news);
        int i = newsMapper.addNews(news);
        if (i > 0) {
            return CommonResult.success("添加成功");
        } else {
            return CommonResult.failed("添加失败");
        }
    }

    @PutMapping("/updateNews")
    @Override
    public CommonResult updateNews(Long id, NewsDTO news) {
        logger.info("入参：" + id + news);
        int i = newsMapper.updateNews(id, news);
        if (i > 0) {
            return CommonResult.success("修改成功");
        } else {
            return CommonResult.failed("修改失败");
        }
    }

    @PutMapping("/updateNewsState")
    @Override
    public CommonResult updateNewsState(Long id, Integer state) {
        logger.info("入参：" + id + state);
        int i = newsMapper.updateNewsState(id, state);
        if (i > 0) {
            return CommonResult.success("修改成功");
        } else {
            return CommonResult.failed("修改失败");
        }
    }

    @GetMapping("/findNewsBack")
    @Override
    public CommonResult findNewsBack(NewsQuery newsQuery) {
        logger.info("入参：" + newsQuery);
        return CommonResult.success(newsMapper.findNewsBack(newsQuery));
    }
}
